package com.chukun.concurrency.immutable;

import com.chukun.concurrency.annotations.UnThreadSafe;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 使用jdk提供的Collections.unmodifiableXXX方法，创建不可变的对象
 *  使用guava的Immutable集合类，创建不可变的对象
 */
@UnThreadSafe
public class ImmutableCollection {

    private final static Integer a=1;
    private final static String b="c";

    //使用jdk的方法，创建不可变的集合
    private static final List list2 = Collections.unmodifiableList(Arrays.asList(1,2,3));


    //使用guava创建不可变的集合
    private static final ImmutableList list = ImmutableList.of(1,2,3);

    private static final ImmutableSet set = ImmutableSet.of(list);

    private static final ImmutableMap<Integer,Integer> map = ImmutableMap
                                                      .<Integer,Integer>builder().put(1,2).put(3,4).build();



    public static void main(String[] args) {
//        a=1;  报错
//        b="d";  报错
    }
}
